﻿using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace BWServices
{
    /// <summary>
    /// Summary description for bwservice
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class bwservice : System.Web.Services.WebService
    {
        static DataManager manager = new DataManager();


        [WebMethod]
        public int UserLogin(string user,string pwd)
        {
            int result = manager.command_int("user_login @user,@pwd", new object[] {user,pwd});
            return result;
        }

        [WebMethod]
        public DataTable UserInfo(int id)
        {
            return manager.command_table("get_user_info @id", new object[] { id });
        }

        [WebMethod]
        public int UploadReportFile(byte[] data,string filename)
        {
            string path = string.Format("{0}\\{1}", Properties.Settings.Default.report_directory, filename);
            FileStream fs = new FileStream(path, FileMode.Create);
            fs.Write(data, 0, data.Length);
            fs.Close();
            return 0;
        }

        [WebMethod]
        public int UploadReport(int usr_id, string number, string user_name, string sex, string hand, DateTime create_date, string memo, string occu, string evalu, int testnumber, DateTime birthdate,string eb)
        {
            int result = manager.command_int("new_report @usrid,@num,@user_name,@sex,@handedness,@create_date,@memo,@occup,@evalu,@testnumber,@birthdate,@eb", 
                new object[] {usr_id,number,user_name,sex,hand,create_date,memo,occu,evalu,testnumber,birthdate,eb});

            return result;
        }

        [WebMethod]
        public int CheckState(int userid,string number)
        {
            int result = manager.command_int("check_report_state @usr,@num", new object[] {userid,number});

            return result;
        }
        [WebMethod]
        public void UpdateState(int userid, string number,int state)
        {
            int result=manager.command_int("update_report_state @usrid,@number,@state", new object[] { userid,number,state});
        }

        [WebMethod]
        public string GetReportURL(int userid, string number)
        {
            return "";
        }

        [WebMethod]
        public decimal GetMoney(int id)
        {
            decimal money = manager.command_decimal("have_money @id", new object[] { id });
            return money;
        }
        [WebMethod]
        public string[] GetReports(int usrid,string username,string testname,string number)
        {
            decimal money = GetMoney(usrid);
            if (money <= 0) return null;
            string filter = string.Format("{0}_{1}_{2}*.doc", username, testname, number);
            string[] reports = Directory.GetFiles(Properties.Settings.Default.DocDirectory,filter);

            for(int i=0;i<reports.Length;i++)
            {
                string item = reports[i];
                FileInfo fi = new FileInfo(item);
                string u = string.Format("{0}/{1}", Properties.Settings.Default.VDocDirectory, fi.Name);
                reports[i] = u;
            }

            return reports;
        }
        [WebMethod]
        public DataTable GetNormalTypes()
        {
            return manager.command_table("get_normal_types",null);
        }
    }
}
